<template>
  <div>
    <div class="breadcrumbArea">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>首页</el-breadcrumb-item>
        <el-breadcrumb-item>模型管理</el-breadcrumb-item>
        <div class="buttonArea">
          <el-row>
            <el-button type="primary" size="small" @click="handleAdd()"
              >新增</el-button
            >
          </el-row>
        </div>
      </el-breadcrumb>
    </div>

    <div class="cantainer">
      <el-table :data="tableData" border stripe style="width: 100%">
        <el-table-column prop="id" label="id" width="80"> </el-table-column>
        <el-table-column prop="name" label="模型名称" width="200">
        </el-table-column>
        <el-table-column prop="modelType" label="模型类别" width="200">
          <template v-slot="scope">
            {{ getModelType(scope.row.modelType) }}
          </template>
        </el-table-column>
        <el-table-column prop="filePath" label="文件路径" width="200">
        </el-table-column>
        <el-table-column prop="createTime" label="创建日期" width="300">
        </el-table-column>
        <el-table-column prop="comment" label="备注"> </el-table-column>
        <el-table-column fixed="right" width="180" label="操作">
          <template v-slot="scope">
            <el-button
              @click="handleEdit(scope.row.id)"
              type="primary"
              icon="el-icon-edit"
              circle
              size="small"
              disabled
            ></el-button>
            <el-button
              @click="handleDelete(scope.$index, scope.row)"
              type="danger"
              icon="el-icon-delete"
              circle
              size="small"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-pagination
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 20, 30, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { queryModelList, delModel } from "@/api/model";
import { queryModelTypeList } from "@/api/modelType";
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 5,
      total: 0,
      tableData: [],
      modelTypeList: [],
    };
  },

  created() {
    this.queryList();
    this.queryModelTypeList();
  },

  methods: {
    queryList() {
      queryModelList(this.currentPage, this.pageSize)
        .then((response) => {
          console.log(response);
          this.tableData = response.data.records;
          this.total = response.data.total;
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    queryModelTypeList() {
      queryModelTypeList()
        .then((response) => {
          console.log(response);
          this.modelTypeList = response.data.records;
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    getModelType(modelTypeId) {
      for (var i = 0; i < this.modelTypeList.length; i++) {
        if (this.modelTypeList[i].id == modelTypeId) {
          return this.modelTypeList[i].name;
        }
      }
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.queryList();
    },

    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.queryList();
    },

    handleAdd() {
      console.log("handleAdd");
      this.$router.push("/model/edit");
    },

    handleEdit(val) {
      console.log(`handleEdit: ${val}`);
    },

    handleDelete(index, row) {
      console.log(index);
      console.log(row);
      this.$confirm(`删除${row.name}, 是否继续?`, "提示", {
        type: "warning",
      })
        .then(() => {
          delModel(row.id)
            .then((response) => {
              console.log(response);
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.queryList();
            })
            .catch(function (e) {
              console.log(e);
              this.$message({
                type: "error",
                message: "删除失败!",
              });
            });
        })
        .catch((e) => {
          console.log(e);
        });
    },
  },
};
</script>

<style scoped>
.breadcrumbArea {
  margin-bottom: 10px;
}
.buttonArea {
  float: right;
}
</style>
